import java.util.List;

/**
 * 使用双向链表 实现的普通队列
 */

public class MyQueue {

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    public int usedSize;

    /**
     * 入队  尾插
     * @param val
     */
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        this.usedSize++;
    }

    /**
     * 删除头节点
     * @return
     */
    public int poll() {
        //1.队列为空
        if(head == null) {
            return -1;
        }
        //2.队列只有一个元素
        int val = head.val;
        if(head.next == null) {
            head = null;
            last = null;
        }else {
            head = head.next;
            head.prev = null;
        }
        //删除完成之后 需要更新 size 的值
        this.usedSize--;
        return val;
    }

    /**
     * 获取头节点的值
     * @return
     */
    public int peek() {
        if(head == null) {
            return -1;
        }
        return head.val;
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    public int size() {
        return this.usedSize;
    }
}